// pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rush_name: '',
    rush_num: '',
    rush_no: '',
    rush_ji:[],
    number:'',
    rush_total:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  totoppage:function(){
    wx.navigateTo({
      url: '/pages/top/top',
    })

  },
  clicknum:function(e){
    var that = this;
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType == "none") {
          wx.showToast({
            title: '网络错误请连接网络后在答题',
            icon: 'none',
            duration: 2000
          })
          wx.reLaunch({
            url: '/pages/list/list',
          })
        }else{

        }
      },
    })
    wx.request({
      url: 'https://sdb.doways.cn/qa/GetUserInfo.do',
      data: {
        openid: wx.getStorageSync('openid'),
      },
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var num = Number(e.target.id)
        console.log("NUM:    " + num)
        if (num > that.data.number) {
          wx.showToast({
            title: '你还没有资格挑战下一关',
            icon: 'none',
            duration: 2000
          })
        } else {
          console.log("that.data.user_guanka            " + that.data.rush_no)
          wx.navigateTo({
            url: '/pages/rush/rush?qanumber=' + num + '&guanka=' + that.data.rush_no,
          })
        }
        
      },
      fail:function(){
        wx.showToast({
          title: '网络错误请连接网络后在答题',
          icon: 'none',
          duration: 2000
        })

      }


    })
    

  },
  onLoad: function (options) {

  
    // wx.request({
    //   url: 'https://sdb.doways.cn/qa/GetGuankaTotal.do',
    //   data: {
    //     wx_openid: wx.getStorageSync('openid'),
    //   },
    //   method: 'post',
    //   headers: {
    //     'Content-Type': 'application/json'
    //   },
    //   success: function (res) {
    //     var array = new Array()
    //     var n = res.data.count
    //     for (var i = 1; i <= n; i++) {
    //       array[i - 1] = i;
    //     }
    //     that.setData({
    //       rush_ji: array
    //     })
    //   }

    // })

    

  //  wx.authorize({ scope: "scope.userInfo" })
    
    
    



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var that = this;
    wx.request({
      url: 'https://sdb.doways.cn/qa/GetUserInfo.do',
      data: {
        openid: wx.getStorageSync('openid'),
      },
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log("res.data:" + res.data)
        wx.setStorageSync('Guanka', res.data.user_guanka)
        console.log("res.data.user_guanka:" + res.data.user_guanka)
        that.setData({
          rush_no: res.data.user_guanka,
          rush_name: res.data.user_stuname,
          rush_num: res.data.user_stunum,
          number: Number(res.data.user_guanka) + 1
        })
        console.log("that.data.number"+that.data.number)

      },
      fail:function(){
        wx.showToast({
          title: '网络错误请连接网络后在答题',
          icon: 'none',
          duration: 2000
        })

      }


    })
    
    wx.request({
      url: 'https://sdb.doways.cn/qa/GetGuankaTotal.do',
      data: {
        wx_openid: wx.getStorageSync('openid'),
      },
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var array = new Array()
        var n = res.data.count
        for (var i = 1; i <= n; i++) {
          array[i - 1] = i;
        }
        console.log(array)
        that.setData({
          rush_ji: array
        })
      },
      fail: function () {
        wx.showToast({
          title: '网络错误请连接网络后在答题',
          icon: 'none',
          duration: 2000
        })

      }

    })
   

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})